package com.dai.code;

/**
 * @author daijiyong
 */
public class FindPeekElement {
    public static void main(String[] args) {

    }

    public static int findPeekElement1(int[] nums) {
        if(nums == null || nums.length == 0){
            return -1;
        }
        for(int i = 1; i < nums.length; i++){
            if( i < nums.length -1 && nums[i] > nums[i -1] && nums[i] > nums[i+1]){
                return i;
            }
        }

        return nums[0] >= nums[nums.length - 1] ? 0 : nums.length -1;
    }

    public static int findPeekElement2(int[] nums) {
        if (nums == null || nums.length == 0) {
            return -1;
        }
        int start = 0;
        int end = nums.length - 1;
        int mid;
        while (start + 1 < end) {
            mid = (start + end) / 2;
            if (nums[mid - 1] > nums[mid]) {
                end = mid;
            } else if (nums[mid + 1] > nums[mid]) {
                start = mid;
            } else {
                return mid;
            }
        }
        return nums[start] > nums[end] ? start : end;
    }
}